package com.journaldev.spring;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.journaldev.spring.model.City;
import com.journaldev.spring.model.PersonalOffice;
import com.journaldev.spring.model.UserInfo;
import com.journaldev.spring.service.PersonalOfficeService;
import com.journaldev.spring.service.UserInfoService;
import com.journaldev.spring.util.SimpleCityEditor;
import com.journaldev.spring.util.SimpleOfficeEditor;

@Controller
public class UserController {
	private UserInfoService userInfoService;
	private PersonalOfficeService officeService;
	
	@InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        binder.registerCustomEditor(PersonalOffice.class, new SimpleOfficeEditor(officeService));
    }

	@Autowired(required=true)
	@Qualifier(value="officeService")
	public void setOfficeService(PersonalOfficeService officeService) {
		this.officeService = officeService;
	}

	@Autowired(required=true)
	@Qualifier(value="userInfoService")
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	@RequestMapping(value = {"/users"}, method = RequestMethod.GET)
	public String listUser(Model model) {
		model.addAttribute("user", new UserInfo());
		model.addAttribute("listUsers", this.userInfoService.listUser());
		model.addAttribute("listOffice", this.officeService.listOffice());
		return "user";
	}
	
	@RequestMapping(value = {"/user/add"}, method = RequestMethod.POST)
	public String addUser(@ModelAttribute("user") UserInfo p,  BindingResult result, Model model, RedirectAttributes redirectAttrs) {
		if(result.hasErrors()) {
			model.addAttribute("message", "add errors");
			return "redirect:/users";
		}
		if(p.getId() != 0) {
			this.userInfoService.updateUserInfo(p);
			redirectAttrs.addFlashAttribute("message", "update success id =" + p.getId());
		} else {
			this.userInfoService.addUserInfo(p);
			redirectAttrs.addFlashAttribute("message", "add success");
		}
		model.asMap().clear();
		return "redirect:/users";
	}
	
	@RequestMapping(value = {"/user/remove/{id}"})
	public String removeUser(@PathVariable("id") int id) {
		this.userInfoService.removeUserInfo(id);
		return "redirect:/users";
	}
	
	@RequestMapping("/user/edit/{id}")
    public String editUser(@PathVariable("id") int id, Model model){
        model.addAttribute("user", this.userInfoService.getUserInfoById(id));
        model.addAttribute("listUsers", this.userInfoService.listUser());
        return "user";
    }
}
